/*
题目描述
约瑟夫环
编号为1，2，3，……，n的n个人按顺时针方向围坐一圈。任选一个正整数作为报数上限m，从第一个人开始按顺时针方向自1开始顺序报数，报到m时停止报数。报m的人出列，从他在顺时针方向上的下一个人开始重新从1报数，如此下去，直至所有人全部出列为止。设计程序输出出列顺序。
输入
人数n 报数上限m
人员记录1 (格式为：姓名 学号 性别 年龄 班级 健康状况)
人员记录2
…
人员记录n
输出
第1次报数出列的人员记录
第2次报数出列的人员记录
…
第n次报数出列的人员记录
样例输入

5 3
安弥邵 10000001 女 28 计43 一般
宰觅 10000002 男 23 计79 健康
顾健 10000003 男 27 计29 一般
宓顽芳 10000004 女 20 计17 健康
能纸垄 10000005 男 18 计11 健康

样例输出

顾健 10000003 男 27 计29 一般
安弥邵 10000001 女 28 计43 一般
能纸垄 10000005 男 18 计11 健康
宰觅 10000002 男 23 计79 健康
宓顽芳 10000004 女 20 计17 健康

*/
#include <iostream>
#include <string>

using namespace std;

class StudentRecord
{
	string stuName;
	int stuNo;
	string stuGender;
	int stuAge;
	string stuClass;
	string stuHealth;
public:
	StudentRecord() {}
	StudentRecord(string stuName,
		int stuNo,
		string stuGender,
		int stuAge,
		string stuClass,
		string stuHealth);
	void print();
};

struct StudentNode
{
	StudentRecord data;
	StudentNode* next;
};

StudentNode* CreatNode(StudentRecord data)
{
	StudentNode* newnode = new StudentNode;
	if (newnode == NULL)
	{
		exit(-1);
	}
	newnode->data = data;
	return newnode;
}

void ListPushBack(StudentNode** pplist, StudentRecord data)
{
	StudentNode* head = *pplist;
	StudentNode* newnode = CreatNode(data);
	newnode->next = NULL;
	if (head == NULL)
	{
		*pplist = newnode;
	}
	else
	{
		while (head->next)
		{
			head = head->next;
		}
		head->next = newnode;
	}
}

void Connect(StudentNode** pplist)
{
	StudentNode* Tail = *pplist;
	while (Tail->next) {
		Tail = Tail->next;
	}
	Tail->next = *pplist;
}

StudentNode* DeleteNode(StudentNode** pplist)
{
	StudentNode* pre = *pplist;
	StudentNode* cur = pre->next; 
	StudentNode* tail = cur->next;
	cur->data.print();
	pre->next = tail;
	delete cur;
	return tail;
}

int main()
{
	StudentNode* pHead = NULL;
	string stuName, stuGender, stuClass, stuHealth;
	int stuNo, stuAge;
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> stuName >> stuNo >> stuGender 
			>> stuAge >> stuClass >> stuHealth;
		StudentRecord data(stuName,
			stuNo,
			stuGender,
			stuAge,
			stuClass,
			stuHealth);
		ListPushBack(&pHead, data);
	}
	Connect(&pHead);
	for (int i = 0; i < n; i++) {
		for (int j = 1; j < m - 1; j++) {
			pHead = pHead->next;
		}
		pHead = DeleteNode(&pHead);
	}
	return 0;
}

StudentRecord::StudentRecord(string stuName, int stuNo, string stuGender, 
	int stuAge, string stuClass, string stuHealth) :
	stuName(stuName), stuNo(stuNo), stuGender(stuGender), 
	stuAge(stuAge), stuClass(stuClass), stuHealth(stuHealth)
{}

void StudentRecord::print()
{
	cout << stuName << " "
		<< stuNo << " "
		<< stuGender << " "
		<< stuAge << " "
		<< stuClass << " "
		<< stuHealth << endl;
}
